package com.ly.test.service.impl;

import com.ly.test.service.IReadFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author luoyun
 * @Date: 2025/5/30 17:57
 * @Note:
 */
@Service
@RequiredArgsConstructor
public class ReadFileServiceImpl implements IReadFileService {

    private final ResourceLoader resourceLoader;

    @Override
    public String readJson(String filePath) throws Exception {
        Resource resource = resourceLoader.getResource("classpath:" + filePath);
        InputStream inputStream = resource.getInputStream();
        byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    // public static void main(String[] args) {
    //     String jsonContent = readFileService.readJson("data/123.json");
    //     List jsonObject = JSONObject.parseObject(jsonContent,List.class);
    // }
}
